/**
 * @file   Int8.cpp
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2014-04-19
 * @brief  DON data type object: Int8
 *
 * Copyright 2014  Djuro Drljaca (djurodrljaca@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <DON/Core/DataType/Int8.h>
#include <QtCore/QByteArray>

using namespace DON::Core::DataType;

class Int8::Private
{
public:
    Private()
        : value(0)
    { }

    qint8 value;
};

Int8::Int8()
    : AbstractDataType(),
      d(new Private())
{
}

Int8::Int8(const Int8 &other)
    : AbstractDataType(),
      d(new Private())
{
    d->value = other.d->value;
}

Int8::~Int8()
{
    delete d;
}

Int8 & Int8::operator=(const Int8 &other)
{
    d->value = other.d->value;

    return *this;
}

AbstractDataType * Int8::clone() const
{
    return new Int8(*this);
}

DataType Int8::getDataType() const
{
    return DataType_Int8;
}

qint8 Int8::getValue() const
{
    return d->value;
}

void Int8::setValue(const qint8 value)
{
    d->value = value;
}

bool Int8::fromBinary(const QByteArray &data, const int startIndex, int *size)
{
    qint8 value = 0;
    bool success = false;

    if ((data.size() > 0) &&
        (startIndex >= 0) &&
        (startIndex < data.size()))
    {
        value = Int8::fromBinary(data.at(startIndex));
        success = true;
    }

    if (success)
    {
        d->value = value;

        if (size != NULL)
        {
            *size = 1;
        }
    }

    return success;
}

QByteArray Int8::toBinary() const
{
    return QByteArray(1, Int8::toBinary(d->value));
}

Int8 * Int8::fromAbstractDataType(AbstractDataType *instance)
{
    Int8 *outputInstance = NULL;

    if (instance != NULL)
    {
        if (instance->getDataType() == DataType_Int8)
        {
            outputInstance = static_cast<Int8 *>(instance);
        }
    }

    return outputInstance;
}

qint8 Int8::fromBinary(const char data)
{
    return static_cast<qint8>(data);
}

char Int8::toBinary(const qint8 value)
{
    return static_cast<char>(value);
}
